const { app, dialog } = require("electron")
const fs = require("fs")
const { replaceHomePath, areKeysEqual, isJson } = require("./func.js");
const cslnConfigStart = require("./csln-config.json")
const USER_HOME = process.env.HOME || process.env.USERPROFILE// 获取用户目录
// 获取用户目录下的csln-config.json文件
//let conf
console.log("开始读取配置文件");
let CP

let cslnConfig = cslnConfigStart// 解析成json
//console.log(cslnConfig)
// 替换<HOMEPATH>
cslnConfig.config_path = replaceHomePath(cslnConfig.config_path, USER_HOME) // 替换<HOMEPATH>
cslnConfig.note_path = replaceHomePath(cslnConfig.note_path, USER_HOME) // 替换<HOMEPATH>
CP = cslnConfig.config_path // 配置文件路径
//console.log(cslnConfig)



const creatNoteDir = (path) => {
    fs.mkdirSync(path, { recursive: true })
    fs.mkdirSync(path + "\\" + "note", { recursive: true })
    fs.writeFileSync(path + "\\" + "note\\" + "config.json", "[]", { encoding: "utf8" })
    fs.mkdirSync(path + "\\" + "code", { recursive: true })
    fs.mkdirSync(path + "\\" + "memo", { recursive: true })
    // 创建配置文件config.json
    fs.writeFileSync(path + "\\" + "memo\\" + "config.json", "[]", { encoding: "utf8" })
    fs.mkdirSync(path + "\\" + "img", { recursive: true })
}

module.exports = {
    config: () => {
        return JSON.parse(fs.readFileSync(CP))
    },
    USER_HOME: USER_HOME,
    changeConfig: (newConfig) => {
        fs.writeFileSync(CP, JSON.stringify(newConfig))
    },
    init() {
        return new Promise((resolve, reject) => {
            // 检查配置文件路径是否存在
            fs.exists(cslnConfig.config_path, function (is_o) { // 判断文件是否存在
                if (is_o) {
                    console.log("配置文件路径存在")
                    // 读取配置文件
                    let config = fs.readFileSync(cslnConfig.config_path)
                    // confing和cslnConfig比对，检查配置文件是否完整
                    if (areKeysEqual(cslnConfig, config)) {

                        console.log("配置文件完整")
                        
                        //conf = config
                        resolve(config)
                    } else {
                        console.log("配置文件不完整")
                        // 弹出确认框
                        const d = dialog.showMessageBoxSync({ // 同步弹出确认框
                            type: "warning",
                            title: "警告",
                            message: "是不是偷偷改配置文件了？下次别干这种事了哦~为了惩罚你，我会重新创建配置文件。请手动重启软件。",
                            buttons: ["好吧，我保证下次不会了", "你管我"]
                        })
                        if (d === 0) {
                            // 创建配置文件,并写入配置文件
                            fs.writeFileSync(cslnConfig.config_path, JSON.stringify(cslnConfig))
                            //conf = cslnConfig
                            app.quit()
                        } else {
                            app.quit()
                        }
                    }
                } else {
                    console.log("配置文件路径不存在")
                    // 创建文件
                    console.log(cslnConfig.config_path);
                    let p = cslnConfig.config_path
                    let p1 = p.split("\\")
                    p1.pop()
                    console.log(p1)
                    fs.mkdirSync(p1.join("\\"), { recursive: true })
                    fs.writeFileSync(cslnConfig.config_path, JSON.stringify(cslnConfig))
                    //conf = cslnConfig
                    creatNoteDir(cslnConfig.note_path)
                    resolve(cslnConfig)
                }
                
            })
        })
    }
}
